package com.ias.agdis_r.activity.setting;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseKeyActivity;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.util.S;

/**
 * 设置菜单界面
 * @author zhangsc
 *
 */
public class SettingActivity extends BaseKeyActivity implements OnClickListener{

	private ActionBar actionBar;
	private TextView txt_net_selected;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settting);
		actionBar = getActionBar();
		initActionBar();
		txt_net_selected = (TextView)findViewById(R.id.txt_net_selected);
		findViewById(R.id.layout_ip).setOnClickListener(this);
		findViewById(R.id.layout_net).setOnClickListener(this);
		findViewById(R.id.layout_gps).setOnClickListener(this);
		findViewById(R.id.layout_keyPress).setOnClickListener(this);
		findViewById(R.id.actionbar_settings_back).setOnClickListener(this);
		loadData();
	}	
	
	@SuppressLint("InflateParams")
	private void initActionBar() {
		actionBar.setCustomView(LayoutInflater.from(SettingActivity.this)
				.inflate(R.layout.actionbar_settings, null));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
		TextView title = (TextView) findViewById(R.id.txt_title);
		title.setText(R.string.SettingActivity);
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		loadData();
	}

	private void loadData()
	{
		String netList = DataBaseManager.getInstance().getString(ParameterModel.T_NET_SELECT_INFO,
				ParameterModel.NET_LIST);
		String netListName;
		if(S.isEmpty(netList))
		{
			netListName = getString(R.string.net_no_choose);
		}
		else
		{
			netListName = netList;
			String[] strs = netList.split(",");
			for (String str : strs) {
				if (ParameterModel.PUBLIC_TAB_NUM.equals(str)) {
					netListName = netListName.replace(ParameterModel.PUBLIC_TAB_NUM, getString(R.string.pub_name));
				} else if (ParameterModel.PRIVATE_TAB_NUM.equals(str)) {
					netListName = netListName.replace(ParameterModel.PRIVATE_TAB_NUM, getString(R.string.pri_name));
				} else if (ParameterModel.WIFI_TAB_NUM.equals(str)) {
					netListName = netListName.replace(ParameterModel.WIFI_TAB_NUM, getString(R.string.wifi_name));
				}
			}
		}
		txt_net_selected.setText(netListName);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layout_ip:
			Intent intent = new Intent(this, IPSettingActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		case R.id.layout_net:
			Intent intent2 = new Intent(this, NetChooseActivity.class);
			intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent2);
			break;
		case R.id.layout_gps:
			// 如果有GPS信息权限
			//或者是管理员
			if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_GPSINFO.toString()) || JavaBeanManager.getInstance().getUserinfo().getIsadmin()) {
				Intent intent3 = new Intent(this, GPSSettingActivity.class);
				intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent3);
			} else {
				EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_UPTO, getString(R.string.no_AI_T_GPSINFO), false);
			}
			break;
		case R.id.actionbar_settings_back:
			finish();
			break;
		case R.id.layout_keyPress:
			startActivity(new Intent(this, KeyPressSettingActivity.class));
			break;
		}
	}
}
